﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Domain
{
	using System;
	using Falcon.Core.Authentication;
	using Falcon.Core.Authorization;
	using Falcon.Core.Collections;
	using Falcon.Core.Domain.Enums;
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;

	public class User : EntityBase<User>
	{
		// private fields
		private UserPreference _preference;
		private UserProfile _profile;

		// public constants
		public const string Guest = "Guests";

		/// <summary>
		/// Initializes a new instance of the <see cref="User"/> class.
		/// </summary>
		public User()
		{
			DeterminePermission(Roles.Users);
		}

		/// <summary>
		/// Gets or sets the preference id.
		/// </summary>
		/// <value>The preference id.</value>
		public virtual int PreferenceId
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the profile id.
		/// </summary>
		/// <value>The profile id.</value>
		public virtual int ProfileId
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the name.
		/// </summary>
		/// <value>The name.</value>
		public virtual string Name
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the slug.
		/// </summary>
		/// <value>The slug.</value>
		public virtual string Slug
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the email address.
		/// </summary>
		/// <value>The email address.</value>
		public virtual string EmailAddress
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the host address.
		/// </summary>
		/// <value>The host address.</value>
		public virtual string HostAddress
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the password.
		/// </summary>
		/// <value>The password.</value>
		public virtual string Password
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the salt.
		/// </summary>
		/// <value>The salt.</value>
		public virtual string Salt
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the posts.
		/// </summary>
		/// <value>The posts.</value>
		public virtual int Posts
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the reputation.
		/// </summary>
		/// <value>The reputation.</value>
		public virtual int Reputation
		{
			get;
			set;
		}

		/// <summary>
		/// Gets a value indicating whether this instance is owner.
		/// </summary>
		/// <value><c>true</c> if this instance is owner; otherwise, <c>false</c>.</value>
		public virtual bool IsOwner
		{
			get
			{
				return Identity != null && this.Id.Equals(Identity.Id);
			}
		}

		/// <summary>
		/// Gets or sets a value indicating whether this instance can update.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance can update; otherwise, <c>false</c>.
		/// </value>
		public override bool CanUpdate
		{
			get 
			{
				return base.CanUpdate && this.IsOwner;
			}
		}

		/// <summary>
		/// Gets or sets the status.
		/// </summary>
		/// <value>The status.</value>
		public virtual UserStatus Status
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the created.
		/// </summary>
		/// <value>The created.</value>
		public virtual DateTime Created
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the updated.
		/// </summary>
		/// <value>The updated.</value>
		public virtual DateTime Updated
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the last visit.
		/// </summary>
		/// <value>The last visit.</value>
		public virtual DateTime LastVisit
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the last log on.
		/// </summary>
		/// <value>The last log on.</value>
		public virtual DateTime LastLogOn
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the preference.
		/// </summary>
		/// <value>The preference.</value>
		public virtual UserPreference Preference
		{
			get
			{
				return this._preference;
			}
			set
			{
				this._preference = value;

				if (this._preference != null)
				{
					this.PreferenceId = this._preference.Id;
				}
			}
		}

		/// <summary>
		/// Gets or sets the profile.
		/// </summary>
		/// <value>The profile.</value>
		public virtual UserProfile Profile
		{
			get
			{
				return this._profile;
			}
			set
			{
				this._profile = value;

				if (this._profile != null)
				{
					this.ProfileId = this._profile.Id;
				}
			}
		}

		/// <summary>
		/// Gets the identity.
		/// </summary>
		/// <value>The identity.</value>
		public static WebUserIdentity Identity
		{
			get
			{
				var principal = PermissivePrincipalPermission.Current as WebPermissivePrincipal;

				if (principal == null)
				{
					return null;
				}

				return principal.Identity as WebUserIdentity;
			}
		}

		/// <summary>
		/// Gets or sets the albums.
		/// </summary>
		/// <value>The albums.</value>
		public virtual IPagedList<UserAlbum, UserAlbumSpecification> Albums
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the friends.
		/// </summary>
		/// <value>The friends.</value>
		public virtual IPagedList<UserContact, UserContactSpecification> Friends
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the groups.
		/// </summary>
		/// <value>The groups.</value>
		public virtual IPagedList<UserGroup, UserGroupSpecification> Groups
		{
			get;
			set;
		}

		/// <summary>
		/// Updates the posts.
		/// </summary>
		public virtual void UpdatePosts()
		{
			UpdatePosts(1, true);
		}

		/// <summary>
		/// Updates the posts.
		/// </summary>
		/// <param name="value">The value.</param>
		/// <param name="addition">if set to <c>true</c> [addition].</param>
		public virtual void UpdatePosts(int value, bool addition)
		{
			if (addition)
			{
				this.Posts += value;
			}
			else
			{
				this.Posts -= value;
			}
		}

		/// <summary>
		/// Updates the reputation.
		/// </summary>
		public virtual void UpdateReputation()
		{
			UpdateReputation(1, true);
		}

		/// <summary>
		/// Updates the reputation.
		/// </summary>
		/// <param name="value">The value.</param>
		/// <param name="addition">if set to <c>true</c> [addition].</param>
		public virtual void UpdateReputation(int value, bool addition)
		{
			if (addition)
			{
				this.Reputation += value;
			}
			else
			{
				this.Reputation -= value;
			}
		}

		/// <summary>
		/// Validation rules for this instance.
		/// </summary>
		/// <returns></returns>
		public override Validator<User> Rules()
		{
			var rules = base.Rules();

			rules.Add(x => x.Name)
				.NotEmpty(Resources.Errors.Required.Fmt(Resources.Titles.Name));

			//rules.Add(x => x.Name)
			//	.Custom(new UserUniqueNameValidator(), Resources.Errors.UserNameTaken);

			rules.Add(x => x.EmailAddress)
				.NotEmpty(Resources.Errors.Required.Fmt(Resources.Titles.EmailAddress));

			//rules.Add(x => x.EmailAddress)
			//	.Custom(new UserUniqueEmailAddressValidator(), Resources.Errors.UserEmailAddressTaken);

			rules.Add(x => x.Password)
				.NotEmpty(Resources.Errors.Required.Fmt(Resources.Titles.Password));

			return rules;
		}

		/// <summary>
		/// Validates this instance.
		/// </summary>
		public override void Validate()
		{
			if (base.SkipValidation)
			{
				return;
			}

			this.Rules().Validate(this);
		}
	}
}